import { createRouter, createWebHistory } from 'vue-router'

export const Layout = () => import('@/views/layout/index.vue')

// 静态路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    name: 'MainPage',
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue'),
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    meta: { hidden: true },
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    hidden: true,
    meta: { title: '首页', icon: 'homepage' },
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: '首页',
        meta: { title: '首页', icon: 'homepage', affix: true },
      },
      {
        path: '401',
        name: 'Error',
        component: () => import('@/views/error-page/401.vue'),
        hidden: true,
        meta: { hidden: true },
      },
      {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        meta: { title: '访问错误', hidden: true },
        component: () => import('@/views/error-page/404.vue'),
        hidden: true,
      },
    ],
  },
  {
    path: '/test',
    component: () => import('@/views/test-code/index.vue'),
    hidden: true,
    meta: { hidden: true },
  },
]

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: '/login' })
  location.reload()
}

export default router
